home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #26 (Nov 87) / asm lab Formatter / FixPtToString.asm < prev    next >
Assembly Source File  |  1987-10-21  |  2KB  |  69 lines

  1. ; FixPtToString.asm
  2. ;-------------------
  3. ; by Mike™ Scanlin  28 Dec 1986
  4.  
  5. Xref    FixPtToString,NumToString
  6.  
  7. ;============
  8. FixPtToString
  9. ;============
  10. ; convert a 32 bit fixed point number into a pascal string
  11. ;  input: D0 fixed point number
  12. ;      D1 16 bit divisor used when D0 was calculated
  13. ;      D2 # of digits after decimal point (D2=0 for no dec point)
  14. ;      A0 points to a space of at least (8 + D2) bytes
  15. ; output: A0 points to pascal string
  16.  
  17.     MOVEM.L        D0-D3/A1,-(SP)
  18.     MOVE.L        D0,D3        ;save quotient & remainder
  19.     EXT.L        D0        ;sign extend quotient
  20.     JSR        NumToString
  21. ;if q = 0 and the result should be < 0, we'll have to add a minus sign
  22. ;(NumToString won't know about it, since all it sees is a zero quotient)
  23.     TST        D3        ;q = 0?
  24.     BNE.S        @0
  25.     TST.L        D3        ;check remainder
  26.     BEQ.S        @0        ;q & r both zero
  27. ;if r & divisor have the same sign, then result will be > 0
  28.     EXT.L        D1
  29.     MOVE.L        D1,D0        
  30.     EOR.L        D3,D0
  31.     BPL.S        @0
  32.     MOVE.B        #'-',1(A0)
  33.     MOVE.B        #'0',2(A0)
  34.     MOVE.B        #2,(A0)        ;new length
  35. @0    TST        D2        ;do we want a decimal point?
  36.     BEQ.S        @6
  37.     MOVEQ        #0,D0
  38.     MOVE.B        (A0),D0        ;length of quotient
  39.     LEA        1(A0,D0),A1    ;end of string + 1
  40.     MOVE.B        #'.',(A1)+
  41.     TST.L        D1        ;make divisor positive
  42.     BPL.S        @1
  43.     NEG.L        D1
  44. @1    TST.L        D3        ;make remainder positive
  45.     BPL.S        @2
  46.     NEG.L        D3
  47. @2    SWAP        D3
  48.     ANDI.L        #$FFFF,D3    ;isolate remainder
  49.     SUBQ        #1,D2        ;loop control
  50. @3    ADD.L        D3,D3        ;mult r by 10
  51.     MOVE.L        D3,D0
  52.     ADD.L        D0,D0        ;4x
  53.     ADD.L        D0,D0        ;8x
  54.     ADD.L        D0,D3        ;10x = 8x + 2x
  55.     MOVEQ        #'0',D0        ;init digit
  56. @4    CMP.L        D1,D3        ;is 10r > divisor?
  57.     BLT.S        @5
  58.     ADDQ        #1,D0        ;increase digit
  59.     SUB.L        D1,D3        ;subtract divisor
  60.     BNE.S        @4
  61. @5    MOVE.B        D0,(A1)+    ;add to string
  62.     DBRA        D2,@3
  63.     MOVE        A1,D0        ;calc length of new string
  64.     SUB        A0,D0
  65.     SUBQ.B        #1,D0        ;minus 1 for length byte
  66.     MOVE.B        D0,(A0)
  67. @6    MOVEM.L        (SP)+,A1/D0-D3
  68.     RTS
  69.